//tab切换
$(function(){
	$('.pay').each(function() {
        $(this).find('.tab_nav li').eq(0).addClass('active');
		$(this).find('.tab_box .tab_cont').eq(0).show().siblings().hide();
		$(this).find('.tab_nav li').click(
			function(){
				$(this).addClass('active').siblings().removeClass('active');
				$(this).parents('.pay').find('.tab_cont').eq($(this).index()).show().siblings().hide();
			}
		)
    });
})
//覆盖整体
function shadboxFun(objId){
	var obj = "#"+objId;  //对象ID
	
	$('.popbox-container').fadeIn();
	$('.popbox-overlay').css({
		"height": $(document).height() + 'px'
	})
	
	var _height = $(obj).height();
	var _width = $(obj).width();
	
	//获取对象距离窗口上部和左部的距离
	var _top = ($(window).height() - _height)/2 + "px";
	var _left = ($(window).width() - _width)/2 + 'px';
	$(obj).css({"left":_left});
	
	$(obj).animate({
		opacity: 'show', top: _top
	}, "200",function(){
		var userAgent = window.navigator.userAgent.toLowerCase();
		var version = $.browser.version;
		
		if(version!="6.0"){//如果是非IE6
			$(obj).css({
				"top": ($(window).height() - _height)/2 + 'px'
			});
		}
		if(version=="6.0"){//如果IE6
			$(obj).css({
				"top": ($(window).height() - _height)/2 + $(window).scrollTop() + 'px'
			});
		}
	});
		
	$('.popbox-close').click(function(){
		$(this).parents('.popbox-wrapper').animate({
			opacity: 'hide',top: '0px'
		}, "slow");
		
		$('.popbox-container').fadeOut();				
	})
	
	$('.popbox-overlay').click(function(){

		$(this).siblings('.popbox-wrapper').animate({
			opacity: 'hide',top: '0px'
		}, "slow");
		$('.popbox-container').fadeOut();				
	})
}

//覆盖整体
function shadboxFun_app(objId){
    var obj = "#"+objId;  //对象ID

    $('.popbox-container').fadeIn();
    $('.popbox-overlay').css({
        "height": $(document).height() + 'px'
    })

    var _height = $(obj).height();
    var _width = $(obj).width();

    //获取对象距离窗口上部和左部的距离
    var _top = ($(window).height() - _height)/6 + "px";
    var _left = ($(window).width() - _width)/2 + 'px';
    $(obj).css({"left":_left});

    $(obj).animate({
        opacity: 'show', top: _top
    }, "200",function(){
        var userAgent = window.navigator.userAgent.toLowerCase();
        var version = $.browser.version;

        if(version!="6.0"){//如果是非IE6
            $(obj).css({
                "top": ($(window).height() - _height)/6 + 'px'
            });
        }
        if(version=="6.0"){//如果IE6
            $(obj).css({
                "top": ($(window).height() - _height)/6 + $(window).scrollTop() + 'px'
            });
        }
    });

    $('.popbox-close').click(function(){
        $(this).parents('.popbox-wrapper').animate({
            opacity: 'hide',top: '0px'
        }, "slow");

        $('.popbox-container').fadeOut();				
    })

    $('.popbox-overlay').click(function(){

        $(this).siblings('.popbox-wrapper').animate({
            opacity: 'hide',top: '0px'
        }, "slow");
        $('.popbox-container').fadeOut();				
    })
}

function set_top_jobs(id,name){
    $("#jobid").val(id);
    $("#jobs_name").val(name);
}
//职位置顶
function job_top(sid,action){
    var jobs_id = $("#jobid").val();
    var jobs_name = $("#jobs_name").val();
    $.post("company.php",{act:'jobs_top',jobs_id:jobs_id,jobs_name:jobs_name,sid:sid,action:action},function(data){
        alert(data.msg);
        if(data.code == 1){
            window.location.reload(); 
        }
    },'json')
}


//取消置顶
function cancel_top_jobs(id){
    $.post("company.php",{act:'cancel_jobs_top',jobs_id:id},function(data){
        alert(data.msg);
        if(data.code == 1){
            window.location.reload(); 
        }
    },'json')
}


//查看打卡(以职位为单位)
function company_sign(id,dkpage,uid){
    $.post("company.php",{act:'get_sign',jobs_id:id,uid:uid,page:dkpage},function(data){
        if(data.code == 1){
            var html = "";
            for(var i=0; i<data.list.length; i++ ){
                var num = i+1;
                html += '<tr><td><em class="ml5">'+num+'</em></td><td>'+data.list[i].mobile+'</td><td>'+data.list[i].sign_time_format+'</td><td>'+data.list[i].sign_address+'</td><td>'+data.list[i].sign_name+'</td>';
                if(data.list[i].sign_way == 1){
                    html += '<td><a href="javascript:void(0);" onclick="open_map('+ data.list[i].map_x +','+ data.list[i].map_y + ','+ data.list[i].sign_time+',\''+data.list[i].sign_address+'\')">查看详情</a></td>';
                }else if(data.list[i].sign_way == 3){
                
                    html += '<td><a href="javascript:void(0);" onclick="open_image(\''+ data.list[i].pic +'\')">查看详情</a></td>';
                }
            }

            if(data.total_page > 1){
                var pagehtml = '<div class="text-center" id="dkpagediv"><ul class="pagination">';
                if(dkpage != 1){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_sign('+id+','+data.front_page+')">上一页</a></li>';
                }

                if(dkpage != data.next_page){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_sign('+id+','+data.next_page+')">下一页</a></li>';
                }
                pagehtml += "</ul></div>";
            }

            $("#daka1").find('tbody').empty();
            $("#daka1").find('tbody').append(html);
            $("#dkpagediv").remove();
            $("#daka1").append(pagehtml);
            shadboxFun('daka1');
        }else{
            alert(data.msg);
        }

    },'json')
}


function open_map(x,y,time,address){
    var url = 'company.php?act=show_sign_map&map_x='+x+'&map_y='+y+'&sign_date='+time+'&address='+address;  //转向网页的地址;
    var name = '打卡地址';                                //网页名称，可为空;
    var iWidth = 960;                                     //弹出窗口的宽度;
    var iHeight = 800;                                    //弹出窗口的高度;
    //获得窗口的垂直位置
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
    //获得窗口的水平位置
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
    window.open(url, name, 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
}


function open_image(pic){
    var url = 'company.php?act=show_sign_pic&pic='+pic;  //转向网页的地址;
    var name = '打卡照片';                                //网页名称，可为空;
    var iWidth = 800;                                     //弹出窗口的宽度;
    var iHeight = 600;                                    //弹出窗口的高度;
    //获得窗口的垂直位置
    var iTop = (window.screen.availHeight - 30 - iHeight) / 2;
    //获得窗口的水平位置
    var iLeft = (window.screen.availWidth - 10 - iWidth) / 2;
    window.open(url, name, 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');
}


function get_applytime(time,state,obj){
    var arr = time.split(',');
    var html = '';
    var n = 1;
    for(var i=0; i<arr.length; i++){
        html += '<span class="btn btn-success ml10 btn-sm mt10">'+arr[i]+'</span>';
        if(n % 6 == 0){
            html += '<br>';
        }
        n ++;
    }

    if(obj != 'undefined'){
        $(obj).closest('tr').find('input').attr('checked',true);
    }

    if(state == 0){
        html += '<p class="text-center mt20"><button class="btn btn-success mr15" onclick="company_agree_refuse(1)">同意</button><button class="btn btn-default" onclick="company_agree_refuse(2)">拒绝</button></p>';
    }else{
        html += '<p class="text-center mt20"></p>';
    }
    
    $("#topjob_apply_time").empty();
    $("#topjob_apply_time").append(html);
    shadboxFun('topjob');
}
//查看报名时间弹出框
function look_applytime(time,state,personal_uid,jobs_id,obj){
   var arr = time.split(',');
   var s_arr = state.split(',');
    var html = '';
    var n = 1;
    for(var i=0; i<arr.length; i++){
		if(s_arr[i]==1){
			html += '<span class="btn btn-default mr15 bm_con" name="'+arr[i]+'" >'+arr[i].substr(5)+'<i class="bm_yes"></i></span>';
		}
		if(s_arr[i]==2){
		 html += '<span class="btn btn-default mr15 bm_con" name="'+arr[i]+'" onclick="company_apply(this)">'+arr[i].substr(5)+'<i class="bm_no"></i></span>';
		
		}
		if(s_arr[i]==0){
		 html += '<span class="btn btn-default mr15" name="'+arr[i]+'" onclick="company_apply(this)">'+arr[i].substr(5)+'</span>';
		
		}
       
        if(n % 6 == 0){
            html += '<br>';
        }
        n ++;
    }

    if(obj != 'undefined'){
        $(obj).closest('tr').find('input').attr('checked',true);
    }
	if(state.indexOf("0")>-1)   
	{   
		 html += '<p class="text-center mt20"><input type="hidden" id="personal_uid" value="'+personal_uid+'"><input type="hidden" id="jobs_id" value="'+jobs_id+'"><button class="btn btn-success mr15" onclick="company_agree_refuse(1,'+personal_uid+','+jobs_id+')">同意</button><button class="btn btn-default" onclick="company_agree_refuse(2,'+personal_uid+','+jobs_id+')">拒绝</button></p>';  
	} 
       
	html += '<p style="color:#F00;padding:10px"">对号表示已经同意，×号表示已经拒绝</p>'; 
    $("#topjob_apply_time").empty();
    $("#topjob_apply_time").append(html);
    shadboxFun('topjob');
}
//点选时间字符串拼接
function company_apply(obj){	
    var time = $(obj).attr('name');
    var yes_apply_time = $('#yes_apply_time').val();

    if($(obj).hasClass('btn-success')){
        $(obj).removeClass('btn-success').addClass('btn-default');
        $('#yes_apply_time').val(yes_apply_time.replace(time+",",'').replace(time,''));

    }else{
        $(obj).removeClass('btn-default').addClass('btn-success');
        if(yes_apply_time != "" ){
            $('#yes_apply_time').val(yes_apply_time+""+time+",");
        }else{
            $('#yes_apply_time').val(time+",");
        }
    }
}
function show_review_list(uid){
    $('#uid').val(uid);
    shadboxFun('review');
}

function review_user(id,action){
    var uid = $("#uid").val();
    window.location.href = "company.php?act=resume_info&rid="+id+"&action="+action+"&uid="+uid;
}

//查看支付记录
function company_paylog(id,dkpage,uid){
    $.post("company.php",{act:'pay_job_log',jobs_id:id,uid:uid,page:dkpage},function(data){
        if(data.code == 1){
            var html = "";
            for(var i=0; i<data.list.length; i++ ){
                var num = i+1;
                html += '<tr><td><em class="ml5">'+num+'</em></td><td>'+data.list[i].money.substr(1)+'</td><td>'+data.list[i].addtime+'</td>';
            }

            if(data.total_page > 1){
                var pagehtml = '<div class="text-center" id="dkpagediv"><ul class="pagination">';
                if(dkpage != 1){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_paylog('+id+','+data.front_page+','+uid+')">上一页</a></li>';
                }

                if(dkpage != data.next_page){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_paylog('+id+','+data.next_page+','+uid+')">下一页</a></li>';
                }
                pagehtml += "</ul></div>";
            }

            $("#paylog").find('tbody').empty();
            $("#paylog").find('tbody').append(html);
            $("#dkpagediv").remove();
            $("#paylog").append(pagehtml);
            shadboxFun('paylog');
        }else{
            alert(data.msg);
        }

    },'json')
}

function pay_salary(time,money,wage,totalmoney,uid){
    var arr = time.split(',');
    var html = '';
    var n = 1;
    for(var i=0; i<arr.length; i++){
        html += '<span class="btn btn-default mr15" name="'+arr[i]+'" onclick="company_pay_salary(this,'+wage+')">'+arr[i].substr(5)+'</span>';
        if(n % 10 == 0){
            html += '<br>';
        }
        n ++;
    }

    $("#pay_days").children('span').remove();
    $("#pay_days").append(html);
    $("#paymoney").text(money);
    $("#total").text(totalmoney);
    $("#personal_id").val(uid);
    shadboxFun('pay1');
}


function company_pay_salary(obj,wage){
    var time = $(obj).attr('name');
    var paytime = $('#paytime').val();
    var salary = $("#salary").val();

    if($(obj).hasClass('btn-success')){
        $(obj).removeClass('btn-success').addClass('btn-default');
        $('#paytime').val(paytime.replace(time+",",'').replace(time,''));
        if(Number(salary) >= Number(wage)){
            $("#salary").val(Number(salary) - Number(wage));
        }
    }else{
        $(obj).removeClass('btn-default').addClass('btn-success');
        if(paytime != "" ){
            $('#paytime').val(paytime+""+time+",");
        }else{
            $('#paytime').val(time+",");
        }
        $("#salary").val(Number(salary) + Number(wage));
    }
}

//支付工资
function go_pay_salary(){
    var paytime = $('#paytime').val(); 
    var jobs_id = $("#jobs_id").val();
    var uid = $("#personal_id").val();

    if(!paytime){
        alert('请选择支付日期');
        return false;
    }

    $.post('company.php',{act:'company_pay_salary_perPersonDays',paytime:paytime,jobs_id:jobs_id,uid:uid},function(data){
        alert(data.msg);
        if(data.code == 1){
            window.location.reload(); 
        }
    },'json')

}

//邀请人才显示职位
function company_job_list(dkpage){
    var uids =  getCheckboxValue('uid');
    if(uids == ''){
        alert('请选择需要邀请的人才');
        return false;
    }

    $("#review_setmeal").hide();
    $("input['name=uid']:checked").each(function(){
        if($(this).next('input').val() == 0){
            $("#review_setmeal").show();
            return false; 
        } 
    })

    $.post("company.php",{act:'company_job_list',page:dkpage},function(data){
        if(data.code == 1){
            var html = "";
            for(var i=0; i<data.list.length; i++ ){
                var num = i+1;
                html += '<tr><td><input type="radio" onclick="show(\''+data.list[i].starttime+'\',\''+data.list[i].deadline+'\',\''+data.list[i].jobs_name+'\')" name="yq_job" value="'+data.list[i].id+'"><em class="ml5">'+num+'</em></td><td>'+data.list[i].jobs_name+'</td><td>'+data.list[i].addtime+'</td><td>'+data.list[i].address+'</td><td>'+data.list[i].amount+'</td>';
            }

            if(data.total_page > 1){
                var pagehtml = '<div class="text-center" id="dkpagediv"><ul class="pagination">';
                if(dkpage != 1){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_job_list('+data.front_page+')">上一页</a></li>';
                }

                if(dkpage != data.next_page){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="company_job_list('+data.next_page+')">下一页</a></li>';
                }
                pagehtml += "</ul></div>";
            }

            $("#pay").find('tbody').empty();
            $("#pay").find('tbody').append(html);
            $("#dkpagediv").remove();
            $("#queren_invite").before(pagehtml);
            shadboxFun('pay');
        }else{
            alert(data.msg);
        }
    },'json')
}


//工作经历
function jingli(uid,dkpage){

    $.post("company.php",{act:'jingli',uid:uid,page:dkpage},function(data){
        if(data.code == 1){
            var html = "";
            for(var i=0; i<data.list.length; i++ ){
                var num = i+1;
                html += '<tr><td><em class="ml5">'+num+'</em></td><td>'+data.list[i].jobs_name+'</td><td>'+data.list[i].addtime+'</td><td>'+data.list[i].address+'</td>';
            }

            if(data.total_page > 1){
                var pagehtml = '<div class="text-center" id="dkpagediv"><ul class="pagination">';
                if(dkpage != 1){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="jingli('+uid+','+data.front_page+')">上一页</a></li>';
                }

                if(dkpage != data.next_page){
                    pagehtml += '<li><a href="javascript:void(0);" onclick="jingli('+uid+','+data.next_page+')">下一页</a></li>';
                }
                pagehtml += "</ul></div>";
            }

            $("#jingli").find('tbody').empty();
            $("#jingli").find('tbody').append(html);
            $("#dkpagediv").remove();
            $("#jingli").append(pagehtml);
            shadboxFun('jingli');
        }else{
            alert(data.msg);
        }
    },'json')

}

//购买套餐
function buy_setmeal(){
    var setmeal_id = $("#setmeal_id").val();
    var num = $("#buy_nums").val();
    var pwd = $("#pwd").val();
    var cityarr = []; 

    if(!setmeal_id){
        alert('参数错误');
        return false;
    }
    
    if(!pwd){
        alert('请填写登陆密码');
        return false;
    }

    $("input[name='citys']:checked").each(function(i){
       cityarr[i] = $(this).val();
    })

    if(setmeal_id == 1 && $("input[name='citys']:checked").size() == 0){
        alert('请选择城市');
        return false;
    }


    $.post('company.php',{act:'buy_setmeal',id:setmeal_id,num:num,pwd:pwd,cityarr:cityarr},function(data){
        alert(data.msg); 
        if(data.code == 1){
            window.location.href = "company.php?act=company_center";
        }
    },'json')
}


//同意申请的职位
function company_agree_refuse(state,personal_uid,jobs_id){
	var worktime = $('#yes_apply_time').val();
    if(!worktime){
        alert('请选择报名时间');
        return false;
    }

    $.post('company.php',{act:'company_apply_do',jobs_id:jobs_id,personal_uid:personal_uid,worktime:worktime,state:state},function(data){
        if(data.code == 1){
            alert(data.msg);
            window.location.reload();
        }else{
            $("#lookcfinfo").empty();
            var html = "";
            for(var i=0; i<data.msg.length; i++){
                html += "<input type='hidden' name='cfdid' value='"+data.msg[i].did+"'>";
            }
            $("#lookcfinfo").append(html);
            $(".popbox-overlay").click();            
            shadboxFun('lookchongfu');

            
        }
    },'json')
}

//同意申请的职位
function company_agree_refuse_all(state,jobs_id){
    var didarr = []; 
    $("input[name='personal_uid']:checked").each(function(i){
        didarr[i] = $(this).val();
    }) 

    if(didarr.length <= 0){
        alert('请选择报名人员');
        return false;
    }
    
    $.post('company.php',{act:'company_apply_do_all','jobs_id':jobs_id,'personal_uid':didarr,'state':state},function(data){
        if(data.code == 1){
            alert(data.msg);
            window.location.reload();
        }else{
            $("#chongfutable").empty();
            var html = '';
            for(var i=0; i<data.msg.length; i++){
                html += "<tr><td><input type='checkbox' name='cfdid' value='"+data.msg[i].did+"'>"+(Number(i)+1)+"</td><td>"+data.msg[i].name+"</td><td>"+data.msg[i].jobs_name+"</td><td>"+data.msg[i].date+"</td></tr>";
            }

            $("#chongfutable").append(html);
            shadboxFun('chongfu');
        }
    },'json')
}

function forceapply(type){
    var didarr = []; 
    if(type == 'checkbox'){
        $("input[name='cfdid']:checked").each(function(i){
            didarr[i] = $(this).val();
        }) 
    }else{
        $("input[name='cfdid']").each(function(i){
            didarr[i] = $(this).val();
        }) 
    }

    if(didarr.length <= 0){
        alert('请选择报名人员');
        return false;
    }

    $.post('company.php',{act:'company_apply_do_force','aids':didarr},function(data){
        alert(data.msg);
        if(data.code == 1){
            window.location.reload();
        }
    },'json')
}

function checkform(){
    $("#page_num").val(1);
    return true;
}

